package com.example.ch2;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.myapplicationtest4.R;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.ch2_activity_second);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        Intent intent = getIntent();
        String data = intent.getStringExtra("extra_data");
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
        Button button = (Button) findViewById(R.id.ch2_button2);
        button.setOnClickListener(a->{
            Log.i("123456","click button2");
            returnData();
        });

    }

    private void returnData() {
        Intent intent1=new Intent();
        intent1.putExtra("data_return","Hello first activity");
        setResult(RESULT_OK,intent1);
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        returnData();
    }
}